package com.jianghu;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import com.jianghu.helpers.*;

public class LoginPage extends Activity {

	private SharedPreferences sp;
	private String password = "password";
	private String username = "username";
	private EditText userName = null;
	private EditText passWord = null;
	private Button btnSubmit = null;
	private CheckBox Check = null;
	private Button btnReg = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.loginpage);
		userName = (EditText) findViewById(R.id.username);
		passWord = (EditText) findViewById(R.id.password);
		btnSubmit = (Button) findViewById(R.id.login);
		btnReg = (Button) findViewById(R.id.reg);
		Check = (CheckBox) findViewById(R.id.remberlogin);
		sp = getSharedPreferences("UserInfo", 0);
		username = sp.getString(username, "");
		password = sp.getString(password, "");

		if (username != "") {
			userName.setText(username);
			passWord.setText(password);
			Check.setChecked(true);
		}
		final String loginfail = this.getResources().getString(
				R.string.loginfail);
		final String stringnull = this.getResources().getString(
				R.string.stringnull);
		btnSubmit.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				if ((userName.getText().toString().equals(""))
						|| (passWord.getText().toString().equals(""))) {

					Toast.makeText(getApplicationContext(), stringnull,
							Toast.LENGTH_SHORT).show();
				} else {

					if (LoginStatus.gotologin(userName.getText().toString(),
							passWord.getText().toString())) {

						sp = getSharedPreferences("UserInfo",
								Context.MODE_WORLD_WRITEABLE
										| Context.MODE_WORLD_READABLE);
						password = sp.getString(password, "");
						if (password != "") {

						} else if (Check.isChecked() == true) {
							sp.edit()
									.putString("username",
											userName.getText().toString())
									.commit();
							sp.edit()
									.putString("password",
											passWord.getText().toString())
									.commit();
							Intent intent = new Intent();
							intent.setClass(LoginPage.this, MainPage.class);
							LoginPage.this.startActivity(intent);
						} else {
							Intent intent = new Intent();
							intent.setClass(LoginPage.this, MainPage.class);
							LoginPage.this.startActivity(intent);
						}
					} else {
						Toast.makeText(getApplicationContext(), loginfail,
								Toast.LENGTH_SHORT).show();
					}
				}
			}

		});
		btnReg.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Uri uri = Uri
						.parse("http://weibo.com/?c=spr_web_sq_baidus_weibo_t400022");
				Intent it = new Intent(Intent.ACTION_VIEW, uri);
				startActivity(it);

			}

		});

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			new AlertDialog.Builder(LoginPage.this).setTitle(R.string.systeminfo).setMessage(R.string.is_sure_cancel).setPositiveButton(R.string.OK,new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Intent intent = new Intent();
					intent.setClass(LoginPage.this, FirstPage.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					intent.putExtra("close", "close");
					startActivity(intent);
				}
			}).setNegativeButton(R.string.cancel, null).show();
		}
		return super.onKeyDown(keyCode, event);
	}
}
